package cn.com.czpb.client.module.personal;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.module.dialog.ShareDialog;
import cn.com.czpb.client.module.personal.acticity.LoginActivity;
import cn.com.czpb.client.utils.ImageUtils;
import cn.com.czpb.client.utils.SPUtil;

/**
 * Created by Administrator on 2017/12/4.
 */

public class TopInvite implements View.OnClickListener {
    private View rootView;
    private TextView mycode;
    private Button goinvite;
    private Button gologin;
    private TextView myinvitecount;
    private LinearLayout invitecodell;
    private RelativeLayout invitecountrl;
    private Activity mActivity;
    private SPUtil spUtil = SPUtil.getInstance();
    private String code;
    public static TopInvite instance(Activity activity){

        return new TopInvite(activity);
    }
    public TopInvite(Activity activity){
        //
        this.mActivity = activity;
        initView(activity);
        initEvent();
    }

    private void initEvent() {
        goinvite.setOnClickListener(this);
        gologin.setOnClickListener(this);
    }

    public View getView(){
        return rootView;
    }
    /**
     * @param context
     */
    private void initView(Activity context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        rootView = layoutInflater.inflate(R.layout.header_invite,null);
        mycode = rootView.findViewById(R.id.mycode);
        goinvite = rootView.findViewById(R.id.goinvite);
        gologin = rootView.findViewById(R.id.gologin);
        invitecodell = rootView.findViewById(R.id.invite_code_ll);
        myinvitecount = rootView.findViewById(R.id.myinvitecount);
        invitecountrl = rootView.findViewById(R.id.invitecountrl);
        if(spUtil.getUser() != null){
            invitecountrl.setVisibility(View.VISIBLE);
            invitecodell.setVisibility(View.VISIBLE);
            goinvite.setVisibility(View.VISIBLE);
            gologin.setVisibility(View.GONE);
        }else {
            invitecountrl.setVisibility(View.GONE);
            invitecodell.setVisibility(View.GONE);
            goinvite.setVisibility(View.GONE);
            gologin.setVisibility(View.VISIBLE);
        }
    }

    public void showLoginStatus(String code){
        this.code = code;
        invitecodell.setVisibility(View.VISIBLE);
        invitecountrl.setVisibility(View.VISIBLE);
        goinvite.setVisibility(View.VISIBLE);
        gologin.setVisibility(View.GONE);
        mycode.setText(code);
    }

    public void showNoLoginStatus(){
        this.code = "000000";
        invitecodell.setVisibility(View.GONE);
        goinvite.setVisibility(View.GONE);
        gologin.setVisibility(View.VISIBLE);
        myinvitecount.setVisibility(View.GONE);
        mycode.setText(code);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.goinvite:
                ImageUtils.saveDrawableById(R.drawable.share_code,"shareapklogo", Bitmap.CompressFormat.JPEG,mActivity);
//                String sharelogourl = Environment.getExternalStorageDirectory()+"/shareapklogo";
                ShareDialog dialog = new ShareDialog(mActivity, R.style.Sharedialog, "邀请码【" + code + "】。下载广西云,从这里爱上广西",
                        InterfaceJsonfile.CODE_SHARE + "/uid/" + SPUtil.getInstance().getUser().getUid() + "/siteid/1/invitcode/" + code, InterfaceJsonfile.PATH_ROOT_INVITE, 0,InterfaceJsonfile.PATH_ROOT_INVITE);
                dialog.show();
                break;
            case R.id.gologin:
                Intent intent = new Intent(mActivity, LoginActivity.class);
                mActivity.startActivity(intent);
                break;
        }
    }
    public void setinvitnum(String num){
        myinvitecount.setText(num);
    }
}
